package com.framework.service.module.user.impl;

import com.framework.common.model.response.R;
import com.framework.common.util.other.NumeralUtil;
import com.framework.mapper.module.user.UserLoginLogMapper;
import com.framework.model.module.user.UserLoginLog;
import com.framework.model.system.vo.SystemUserVo;
import com.framework.service.base.BaseService;
import com.framework.service.module.user.UserLoginLogService;
import java.util.Date;
import java.util.List;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

/**
 * @author 龘鵺
 * @version 1.0
 * @className com.framework.service.module.user.impl.UserLoginLogServiceImpl
 * @description 用户登录记录业务接口实现类
 * @datetime 2024-06-20 20:28:00
 */
@Service("userLoginLogServiceImpl")
public class UserLoginLogServiceImpl extends BaseService implements UserLoginLogService {
    private Logger log = LoggerFactory.getLogger(UserLoginLogServiceImpl.class);
    @Autowired
    private UserLoginLogMapper userLoginLogMapper;

    /**
     * @param id 1 编号
     * @return com.framework.model.module.user.UserLoginLog
     * @title 根据编号查询用户登录记录
     * @description 根据编号查询用户登录记录
     * @author 龘鵺
     * @datetime 2024-06-20 20:28:00
     * @version 1.0
     */
    @Override
    public UserLoginLog getByIdParam(Long id) {
        UserLoginLog row = new UserLoginLog();
        row.setId(id);
        UserLoginLog p = userLoginLogMapper.selectByParam(row);
        if (p == null) {
            return new UserLoginLog();
        }
        return p;
    }

    /**
     * @param row 1 用户登录记录类
     * @return com.framework.common.model.response.R
     * @title 新增用户登录记录
     * @description 新增用户登录记录
     * @author 龘鵺
     * @datetime 2024-06-20 20:28:00
     * @version 1.0
     */
    @Override
    @Transactional
    public R<?> save(UserLoginLog row) {
        SystemUserVo systemUserVo = getUser();
        Long userId = systemUserVo.getId();
        Date date = new Date();
        
        
        row.setOperaterId(userId);
        row.setOperaterTime(date);
        userLoginLogMapper.insertSelective(row);
        return R.ok();
    }

    /**
     * @param row 1 用户登录记录类
     * @return com.framework.common.model.response.R
     * @title 编辑用户登录记录
     * @description 编辑用户登录记录
     * @author 龘鵺
     * @datetime 2024-06-20 20:28:00
     * @version 1.0
     */
    @Override
    @Transactional
    public R<?> edit(UserLoginLog row) {
        UserLoginLog queryParam = userLoginLogMapper.selectByPrimaryKey(row.getId());
        if (queryParam == null) {
            return R.fail("编辑信息不存在!");
        }
        SystemUserVo systemUserVo = getUser();
        Long userId = systemUserVo.getId();
        Date date = new Date();
        row.setOperaterTime(date);
        row.setOperaterId(userId);
        userLoginLogMapper.updateByPrimaryKeySelective(row);
        return R.ok();
    }

    /**
     * @param id 1 编号
     * @return com.framework.common.model.response.R
     * @title 删除用户登录记录
     * @description 删除用户登录记录
     * @author 龘鵺
     * @datetime 2024-06-20 20:28:00
     * @version 1.0
     */
    @Override
    @Transactional
    public R<?> delete(Long id) {
        UserLoginLog row = new UserLoginLog();
        SystemUserVo systemUserVo = getUser();
        Long userId = systemUserVo.getId();
        Date date = new Date();
        row.setId(id);
        row.setOperaterTime(date);
        row.setOperaterId(userId);
        userLoginLogMapper.delete(row);
        return R.ok();
    }

    /**
     * @param idList 1 编号集合
     * @return com.framework.common.model.response.R
     * @title 批量删除用户登录记录
     * @description 批量删除用户登录记录
     * @author 龘鵺
     * @datetime 2024-06-20 20:28:00
     * @version 1.0
     */
    @Override
    @Transactional
    public R<?> batchDeleteList(List<Long> idList) {
        UserLoginLog row = new UserLoginLog();
        SystemUserVo systemUserVo = getUser();
        Long userId = systemUserVo.getId();
        Date date = new Date();
        row.setIdList(idList);
        row.setOperaterTime(date);
        row.setOperaterId(userId);
        //userLoginLogMapper.deleteList(row);
        return R.ok();
    }

    /**
     * @param row 1 用户登录记录类
     * @return java.util.List<com.framework.model.module.user.UserLoginLog>
     * @title 查询用户登录记录集合
     * @description 查询用户登录记录集合
     * @author 龘鵺
     * @datetime 2024-06-20 20:28:00
     * @version 1.0
     */
    @Override
    public List<UserLoginLog> findByList(UserLoginLog row) {
        List<UserLoginLog> list = userLoginLogMapper.findByList(row);
        return list;
    }

    /**
     * @param row 1 用户登录记录类
     * @return com.framework.common.model.response.R
     * @title 分页查询用户登录记录列表
     * @description 分页查询用户登录记录列表
     * @author 龘鵺
     * @datetime 2024-06-20 20:28:00
     * @version 1.0
     */
    @Override
    public R<?> findParamPageList(UserLoginLog row) {
        SystemUserVo systemUserVo = getUser();
        if (!super.ignoredRoleAuth.getAuthList().contains(systemUserVo.getRoleCode())) {
            row.setSources(systemUserVo.getSources());
        }
        int count = userLoginLogMapper.findPageListCount(row);
        if (count == NumeralUtil.POSITIVE_ZERO) {
            return R.ok();
        }
        List<UserLoginLog> list = userLoginLogMapper.findPageList(row);
        return R.ok(list, count);
    }
}